package network;

import java.io.InputStream;
import java.net.Socket;

import controller.Constants;

/**
 * A Message receiver which is used to receive message on a socket
 * 
 * @author Jie Zhao(288654)
 * 
 */
public class MessageReceiver {
	private byte[] buffer = new byte[65535];

	/**
	 * receive a message on the given socket and return an object of that
	 * message
	 * 
	 * @param s
	 * @return
	 * @throws Exception
	 */
	public Message receiveMessage(Socket s) throws Exception {
		InputStream is = s.getInputStream();

		int leng = is.read(buffer);
		if (leng == -1) {
			if (Constants.DEBUG)
				System.out.println("EOF reach,Exit");
			throw new Exception("EOF Reached");
		}
		Message m = Message.parseMessage(buffer);
		if (Constants.DEBUG)
			System.out
					.println("Receive from Socket(" + s.toString() + "):" + m);
		return m;
	}
}
